package com.wx.mall.operate.controller;


import com.alibaba.fastjson.JSON;
import com.wx.mall.common.api.CommonPage;
import com.wx.mall.common.api.CommonResult;
import com.wx.mall.common.api.ResultCode;
import com.wx.mall.common.exceptions.ApiAssert;
import com.wx.mall.operate.dto.ActivityBatchDto;
import com.wx.mall.operate.dto.ActivityDto;
import com.wx.mall.operate.entity.Activity;
import com.wx.mall.operate.service.IActivityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author j
 * @since 2020-05-07
 */
@RestController
@RequestMapping("/activity")
public class ActivityController {

    @Autowired
    IActivityService activityService;

    @ApiOperation("列表")
    @GetMapping("/list")
    public CommonResult<List<Activity>> list(){
        List<Activity> list = activityService.list();
        return CommonResult.success(list);
    };


    @ApiOperation("根据id查询数据")
    @GetMapping("/listById")
    public CommonResult<Activity> listById(Integer id){
        Activity list = activityService.getById(id);
        return CommonResult.success(list);
    };
    @ApiOperation("获取数据列表（带筛选和排序）")
    @GetMapping("/getList")
    public CommonResult<CommonPage<Activity>> getList(@ApiParam("获取dto数据") ActivityDto dto){
        System.out.println(dto);
        if (dto.getPageNum() == null || dto.getPageSize() == null) {
            ApiAssert.fail(ResultCode.VALIDATE_FAILED); // 直接抛异常，使用enum
        }
        if (dto.getPageNum() <= 0) {
            ApiAssert.fail("无效页码"); // 直接抛异常，使用自定义的message
        }

        List<Activity> list=activityService.getList(dto);

        return CommonResult.success(CommonPage.restPage(list));


    };

    @ApiOperation("更新一条数据")
    @PostMapping("/updateOne")
    public CommonResult updateOne(Activity activity){


        boolean sucess=activityService.updateById(activity);
        return sucess?CommonResult.success():CommonResult.failed();

    };


    @ApiOperation("添加一条数据")
    @PostMapping("/addOne")
    public CommonResult addOne(Activity activity){


        boolean sucess=activityService.save(activity);
        return sucess?CommonResult.success():CommonResult.failed();

    };


    @ApiOperation("删除数据")
    @DeleteMapping("/deleteList")
    public CommonResult deleteList(ActivityBatchDto dto){

        System.out.println(dto);
        boolean sucess=activityService.removeByIds(dto.getIds());
        return sucess?CommonResult.success():CommonResult.failed();

    };

    @ApiOperation("上传图片")
    @PostMapping("/uploadImage")
    public CommonResult uploadImage(@RequestParam("file")MultipartFile file,@RequestParam Map<String,String> param){


        String name=param.get("title").trim();

        Resource resource=file.getResource();
Map<String,String> map=activityService.uploadImage(file,name);

        return CommonResult.success(map);

    };




}

